@extends('admin.layouts.app')

@section('title', '用户管理 - 挪车小助手后台管理')

@section('content')
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">用户管理</h1>
    <div class="btn-toolbar mb-2 mb-md-0">
        <div class="btn-group me-2">
            <button type="button" class="btn btn-sm btn-outline-secondary" onclick="window.print()">
                <i class="bi bi-printer"></i> 打印
            </button>
            <a href="{{ route('admin.users.export') }}" class="btn btn-sm btn-outline-secondary">
                <i class="bi bi-download"></i> 导出
            </a>
        </div>
    </div>
</div>

<div class="row mb-3">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                用户筛选
            </div>
            <div class="card-body">
                <form action="{{ route('admin.users.index') }}" method="GET" class="row g-3">
                    <div class="col-md-3">
                        <label for="search" class="form-label">搜索</label>
                        <input type="text" class="form-control" id="search" name="search" placeholder="昵称/手机号码" value="{{ request('search') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="date_from" class="form-label">注册日期从</label>
                        <input type="date" class="form-control" id="date_from" name="date_from" value="{{ request('date_from') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="date_to" class="form-label">注册日期至</label>
                        <input type="date" class="form-control" id="date_to" name="date_to" value="{{ request('date_to') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="status" class="form-label">状态</label>
                        <select class="form-select" id="status" name="status">
                            <option value="">全部</option>
                            <option value="1" {{ request('status') == '1' ? 'selected' : '' }}>正常</option>
                            <option value="0" {{ request('status') == '0' ? 'selected' : '' }}>已禁用</option>
                        </select>
                    </div>
                    <div class="col-12">
                        <button type="submit" class="btn btn-primary">筛选</button>
                        <a href="{{ route('admin.users.index') }}" class="btn btn-secondary">重置</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                用户列表
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>头像</th>
                                <th>昵称</th>
                                <th>手机号码</th>
                                <th>注册时间</th>
                                <th>最后登录</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse ($users as $user)
                                <tr>
                                    <td>{{ $user->id }}</td>
                                    <td>
                                        @if($user->avatar_url)
                                            <img src="{{ $user->avatar_url }}" alt="头像" class="rounded-circle" width="40" height="40">
                                        @else
                                            <img src="/images/default-avatar.svg" alt="默认头像" class="rounded-circle" width="40" height="40">
                                        @endif
                                    </td>
                                    <td>{{ $user->nickname }}</td>
                                    <td>{{ $user->phone ?? '未绑定' }}</td>
                                    <td>{{ $user->created_at }}</td>
                                    <td>{{ $user->last_login_at ?? '未登录' }}</td>
                                    <td>
                                        @if($user->is_active)
                                            <span class="badge bg-success">正常</span>
                                        @else
                                            <span class="badge bg-danger">已禁用</span>
                                        @endif
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm" role="group">
                                            <a href="{{ route('admin.users.show', $user->id) }}" class="btn btn-info">
                                                <i class="bi bi-eye"></i>
                                            </a>
                                            <button type="button" class="btn btn-{{ $user->is_active ? 'warning' : 'success' }}" 
                                                data-bs-toggle="modal" 
                                                data-bs-target="#statusModal" 
                                                data-id="{{ $user->id }}" 
                                                data-status="{{ $user->is_active ? 0 : 1 }}"
                                                data-action="{{ $user->is_active ? '禁用' : '启用' }}">
                                                <i class="bi bi-{{ $user->is_active ? 'lock' : 'unlock' }}"></i>
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="8" class="text-center">暂无用户数据</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                <div class="d-flex justify-content-center mt-3">
                    {{ $users->links() }}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 状态修改确认弹窗 -->
<div class="modal fade" id="statusModal" tabindex="-1" aria-labelledby="statusModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="statusModalLabel">确认操作</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                确定要<span id="actionText"></span>这个用户吗？
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="statusForm" action="" method="POST">
                    @csrf
                    @method('PUT')
                    <input type="hidden" id="statusInput" name="status" value="">
                    <button type="submit" class="btn btn-primary">确认</button>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 状态修改弹窗处理
        const statusModal = document.getElementById('statusModal');
        statusModal.addEventListener('show.bs.modal', function (event) {
            const button = event.relatedTarget;
            const userId = button.getAttribute('data-id');
            const status = button.getAttribute('data-status');
            const action = button.getAttribute('data-action');
            
            const actionText = document.getElementById('actionText');
            const statusForm = document.getElementById('statusForm');
            const statusInput = document.getElementById('statusInput');
            
            actionText.textContent = action;
            statusForm.action = `/admin/users/${userId}/status`;
            statusInput.value = status;
        });
    });
</script>
@endsection 